package com.dzsw.netty.websocket;

import com.dzsw.netty.util.ProtobufUtil;
import com.dzsw.netty.websocket.protocal.CProtocol;
import io.netty.channel.*;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

/**
 * 处理前端的登录逻辑,JWT认证成功后更新在线状态并拉取离线消息
 */
@Slf4j
@ChannelHandler.Sharable
public class RequestHandler extends SimpleChannelInboundHandler<CProtocol.Request> {

    public final static RequestHandler INSTANCE = new RequestHandler();

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, CProtocol.Request msg) {
        try {
            CProtocol.Response response = CProtocol.Response.newBuilder()
                    .setData("data")
                    .setStatus(1)
                    .setMsg("success")
                    .build();
            CProtocol.Message message = CProtocol.Message.newBuilder()
                    .setResponse(response)
                    .setType(CProtocol.EnumDataType.RESPONSE)
                    .build();
            log.info(Arrays.toString(message.toByteArray()));
            ProtobufUtil.sendBinaryMessage(ctx, message.toByteArray(), true, (ChannelFutureListener) f ->{
                log.info("message send ok.");
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
